home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / share / gnome-panel / add-indicator-applet.py next >
Text File  |  2009-10-19  |  4KB  |  108 lines

  1. #!/usr/bin/python
  2.  
  3. import os.path
  4. import sys
  5. import time
  6. import gconf
  7.  
  8. def msg(type, primary, secondary=""):
  9.     sys.stderr.write("[%s] %s: %s\n" % (os.path.basename(sys.argv[0]),
  10.                                         type, primary))
  11.     if secondary:
  12.         sys.stderr.write("%s\n\n" % secondary)
  13.  
  14. def error(primary, secondary=""):
  15.     msg("ERROR", primary, secondary)
  16.     
  17. def info(primary, secondary=""):
  18.     msg("INFO", primary, secondary)
  19.  
  20. def log_excepthook(exc_type, exc_obj, exc_tb):
  21.     import traceback
  22.     l = traceback.format_exception(exc_type, exc_obj, exc_tb)
  23.     error("exception","".join(l))
  24.     
  25. def associate_schemas_in_dir(client, engine, profile_dir, schema_dir):
  26.     """ 
  27.     helper that takes a gconf schema dir and creates a new profile dir
  28.     based on the schema, useful for e.g. adding a applet to the panel
  29.     """
  30.     for e in client.all_entries(schema_dir):
  31.         schema_key = os.path.basename(e.get_key())
  32.         key = os.path.join(profile_dir, schema_key)
  33.         engine.associate_schema(key, e.get_key())
  34.     client.suggest_sync()
  35.  
  36. if __name__ == "__main__":
  37.     sys.excepthook = log_excepthook
  38.     info("started")
  39.  
  40.     if not os.path.exists("/usr/lib/indicator-applet/indicator-applet"):
  41.         info("no indicator applet binary installed, exiting")
  42.         sys.exit(0)
  43.  
  44.     engine = gconf.engine_get_default()
  45.     client = gconf.client_get_for_engine(engine)
  46.     #info("engine",engine)
  47.     #info("client",client)
  48.  
  49.     # make sure this is auto-run only once
  50.     client.set_bool("/apps/panel/need_add_indicator_applet", False)
  51.     #info("set /apps/panel/need_add_indicator_applet to false")
  52.  
  53.     # search for the notification area applet
  54.     applets = client.all_dirs("/apps/panel/applets")
  55.     notification_pos = None
  56.     notification_panel = None
  57.     notification_right_stick = False
  58.     for d in applets:
  59.         #info("looking at: %s" % d)
  60.         if (client.get_string(os.path.join(d,"bonobo_iid")) == "OAFIID:GNOME_IndicatorApplet"):
  61.             info("Already has a indicator applet",
  62.                  "Your panel already has a indicator applet")
  63.             sys.exit(0)
  64.         if (client.get_string(os.path.join(d,"bonobo_iid")) == "OAFIID:GNOME_NotificationAreaApplet"):
  65.             notification_pos = client.get_int(os.path.join(d,"position"))
  66.             notification_panel = client.get_string(os.path.join(d,"toplevel_id"))
  67.             notification_right_stick = client.get_bool(os.path.join(d,"panel_right_stick"))
  68.     # check if we found the notification area
  69.     if (notification_pos is None or 
  70.         notification_pos == 0 or
  71.         notification_panel is None):
  72.         error("Could not find notification area",
  73.               "Please add the indicator applet manually")
  74.         sys.exit(1)
  75.  
  76.     info("New post: ", notification_pos)
  77.     info("New panel: ", notification_panel)
  78.  
  79.     # create a new indicator applet
  80.     applet_name = "applet_indicator_auto_added"
  81.     new_applet_name = os.path.join("/apps/panel/applets/",applet_name)
  82.     associate_schemas_in_dir(client, engine, new_applet_name,
  83.                              "/schemas/apps/panel/objects")
  84.     client.set_string(os.path.join(new_applet_name, "bonobo_iid"), 
  85.                       "OAFIID:GNOME_IndicatorApplet")
  86.     client.set_string(os.path.join(new_applet_name, "toplevel_id"), 
  87.                       notification_panel)
  88.     client.set_string(os.path.join(new_applet_name, "object_type"), 
  89.                       "bonobo-applet")
  90.     client.set_bool(os.path.join(new_applet_name, "panel_right_stick"), 
  91.                       notification_right_stick)
  92.     # position depends on if tray_applet is right stick or not
  93.     if notification_right_stick:
  94.     notification_pos += 1
  95.     else:
  96.     notification_pos -= 1
  97.     client.set_int(os.path.join(new_applet_name, "position"), 
  98.                       notification_pos)
  99.     l=client.get_list("/apps/panel/general/applet_id_list", gconf.VALUE_STRING)
  100.     l.append(applet_name)
  101.     client.set_list("/apps/panel/general/applet_id_list", gconf.VALUE_STRING, l)
  102.     client.suggest_sync()
  103.  
  104.     # show nice information
  105.     info("Configuration updated",
  106.          "Your panel configuration is updated. ")
  107.  
  108.